
Security News
Vite Releases Technical Preview of Rolldown-Vite, a Rust-Based Bundler
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
web3-provider-engine
Advanced tools
A JavaScript library for composing Ethereum provider objects using middleware modules
The web3-provider-engine is a JavaScript library that allows developers to create custom Ethereum providers. It is designed to be a modular and extensible provider engine that can be used to manage multiple Ethereum networks and handle various types of requests. It is particularly useful for managing custom signing logic, caching, and other middleware functionalities.
Custom Middleware
This feature allows you to add custom middleware to the provider engine. In this example, we add an RPC subprovider to connect to the Ethereum mainnet via Infura. The engine can be extended with additional middleware to handle custom logic.
const Web3ProviderEngine = require('web3-provider-engine');
const RpcSubprovider = require('web3-provider-engine/subproviders/rpc');
const engine = new Web3ProviderEngine();
engine.addProvider(new RpcSubprovider({ rpcUrl: 'https://mainnet.infura.io' }));
engine.start();
Caching
The caching feature allows you to cache certain types of requests to improve performance. By adding a CacheSubprovider, you can cache responses for specific requests, reducing the number of network calls.
const CacheSubprovider = require('web3-provider-engine/subproviders/cache');
engine.addProvider(new CacheSubprovider());
Custom Signing
This feature allows you to implement custom signing logic. The HookedWalletSubprovider can be used to define how accounts are retrieved and how transactions are signed, enabling custom wallet implementations.
const HookedWalletSubprovider = require('web3-provider-engine/subproviders/hooked-wallet');
engine.addProvider(new HookedWalletSubprovider({
getAccounts: (cb) => cb(null, ['0x1234...']),
signTransaction: (txParams, cb) => cb(null, '0x5678...')
}));
The ethers.js library is a complete and compact library for interacting with the Ethereum blockchain. It provides a more modern and user-friendly API compared to web3-provider-engine, with built-in support for providers, signing, and contract interaction. While web3-provider-engine focuses on middleware and custom provider logic, ethers.js offers a more comprehensive solution for Ethereum development.
The web3.js library is a popular JavaScript library for interacting with the Ethereum blockchain. It provides a wide range of functionalities, including contract interaction, account management, and provider handling. Compared to web3-provider-engine, web3.js offers a more integrated approach with a focus on ease of use and compatibility with the Ethereum ecosystem.
Truffle HDWallet Provider is a provider engine that allows you to use a mnemonic phrase to generate accounts and sign transactions. It is similar to web3-provider-engine in that it provides custom signing capabilities, but it is specifically designed for use with the Truffle suite of tools and focuses on HD wallet functionality.
Web3 ProviderEngine is a tool for composing your own web3 providers.
Originally created for MetaMask, but has been superceded by json-rpc-engine in combination with our eth-json-rpc-middleware. This module is not very actively maintained, so we recommend using that one instead.
Built to be modular - works via a stack of 'sub-providers' which are like normal web3 providers but only handle a subset of rpc methods.
The subproviders can emit new rpc requests in order to handle their own; e.g. eth_call
may trigger eth_getAccountBalance
, eth_getCode
, and others.
The provider engine also handles caching of rpc request results.
const ProviderEngine = require('web3-provider-engine')
const CacheSubprovider = require('web3-provider-engine/subproviders/cache.js')
const FixtureSubprovider = require('web3-provider-engine/subproviders/fixture.js')
const FilterSubprovider = require('web3-provider-engine/subproviders/filters.js')
const VmSubprovider = require('web3-provider-engine/subproviders/vm.js')
const HookedWalletSubprovider = require('web3-provider-engine/subproviders/hooked-wallet.js')
const NonceSubprovider = require('web3-provider-engine/subproviders/nonce-tracker.js')
const RpcSubprovider = require('web3-provider-engine/subproviders/rpc.js')
var engine = new ProviderEngine()
var web3 = new Web3(engine)
// static results
engine.addProvider(new FixtureSubprovider({
web3_clientVersion: 'ProviderEngine/v0.0.0/javascript',
net_listening: true,
eth_hashrate: '0x00',
eth_mining: false,
eth_syncing: true,
}))
// cache layer
engine.addProvider(new CacheSubprovider())
// filters
engine.addProvider(new FilterSubprovider())
// pending nonce
engine.addProvider(new NonceSubprovider())
// vm
engine.addProvider(new VmSubprovider())
// id mgmt
engine.addProvider(new HookedWalletSubprovider({
getAccounts: function(cb){ ... },
approveTransaction: function(cb){ ... },
signTransaction: function(cb){ ... },
}))
// data source
engine.addProvider(new RpcSubprovider({
rpcUrl: 'https://testrpc.metamask.io/',
}))
// log new blocks
engine.on('block', function(block){
console.log('================================')
console.log('BLOCK CHANGED:', '#'+block.number.toString('hex'), '0x'+block.hash.toString('hex'))
console.log('================================')
})
// network connectivity error
engine.on('error', function(err){
// report connectivity errors
console.error(err.stack)
})
// start polling for blocks
engine.start()
When importing in webpack:
import * as Web3ProviderEngine from 'web3-provider-engine';
import * as RpcSource from 'web3-provider-engine/subproviders/rpc';
import * as HookedWalletSubprovider from 'web3-provider-engine/subproviders/hooked-wallet';
The Ethereum JSON RPC was not designed to have one node service many clients. However a smaller, lighter subset of the JSON RPC can be used to provide the blockchain data that an Ethereum 'zero-client' node would need to function. We handle as many types of requests locally as possible, and just let data lookups fallback to some data source ( hosted rpc, blockchain api, etc ). Categorically, we don’t want / can’t have the following types of RPC calls go to the network:
eth-json-rpc-engine
babelify@10
+ @babel/core@7
dist
from git (but published in npm module)dist/es5
hooked-wallet-ethtx
message signinghooked-wallet
default txParamseth-block-tracker
@2.0.0eth-block-tracker
.personalRecoverSigner
to recoverPersonalSignature
pollingShouldUnref
option now defaults to falseFAQs
A JavaScript library for composing Ethereum provider objects using middleware modules
The npm package web3-provider-engine receives a total of 123,624 weekly downloads. As such, web3-provider-engine popularity was classified as popular.
We found that web3-provider-engine demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 12 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
Research
Security News
A malicious npm typosquat uses remote commands to silently delete entire project directories after a single mistyped install.
Research
Security News
Malicious PyPI package semantic-types steals Solana private keys via transitive dependency installs using monkey patching and blockchain exfiltration.